/**
 * Created by shenglu.li on 2014/11/24.
 */
function changeEnv() {
    var own = document.getElementById("childsys");
    if (own.value == "") return false;
    var ip = document.getElementById("serverIp");
    var system = document.getElementById("system");
    var instanceid = document.getElementById("instanceid");
    var link_domain = system.options[system.selectedIndex].value+"_"+own.value+"_"+instanceid.options[instanceid.selectedIndex].value;
    var ajax = new AjaxObj();
    ajax.swRequest({
        method:"POST",
        sync:false,
        url:'?m=getIpList',
        data:"own="+own.value,
        success: function(msg) {
            var ip_arr = new Array();
            ip_arr = msg.split(';');
            $("#serverIp").empty();
            $('#sharing_domain').tagsinput('removeAll');
            for (var i=0; i< ip_arr.length; i++) {
                if (ip_arr[i] == "") continue;
                $("#serverIp").append("<option value='"+ip_arr[i]+"' selected='selected'>"+ip_arr[i]+"</option>");
            }
            $("#serverIp").trigger("chosen:updated");
        },
        failure: function(a) {
            alert(a);
        },
        soap:this
    });
    $("#childsys").attr("data-original-title", "鍩熷悕锛�"+link_domain);
    $("#childsys").mouseout();
    $("#childsys").mouseover();
}
function addDomainItem(domain) {
    var ajax = new AjaxObj();
    ajax.swRequest({
        method:"POST",
        sync:false,
        url:'?m=getIpList',
        data:"own="+domain,
        success: function(msg) {
            var ip_arr = new Array();
            ip_arr = msg.split(';');
            for (var i=0; i< ip_arr.length; i++) {
                $("#serverIp").find("option").each(function(){
                    if ($(this).val() == ip_arr[i]) {
                        $(this).remove();
                    }
                });
                $("#serverIp").append("<option value='"+ip_arr[i]+"' selected='selected'>"+ip_arr[i]+"</option>");
            }
            $("#serverIp").trigger("chosen:updated");
        },
        failure: function(a) {
            alert(a);
        },
        soap:this
    });
}
function removeDomainItem() {
    var own = $("#childsys").val();
    $.ajax({
        type:"POST",
        async:false,
        dataType:"text",
        url:"?m=getIpList",
        data:{"own":own},
        success:function(data,textStatus){
            var ip_arr = new Array();
            ip_arr = data.split(';');
            for (var i=0; i< ip_arr.length; i++) {
                $("#serverIp").append("<option value='"+ip_arr[i]+"' selected='selected'>"+ip_arr[i]+"</option>");
            }
            $("#serverIp").trigger("chosen:updated");
        },
        error:function(XMLHttpRequest,textStatus,errorThrown){

        }
    });
    var sharing_domain = $("#sharing_domain").val();
    if (sharing_domain != '') {
        var domain_arr = new Array();
        domain_arr = sharing_domain.split(',');
        for (var i=0;i < domain_arr.length; i++) {
            $.ajax({
                type:"POST",
                async:false,
                dataType:"text",
                url:"?m=getIpList",
                data:{"own":domain_arr[i]},
                success:function(data,textStatus){
                    var ip_arr = new Array();
                    ip_arr = data.split(';');
                    for (var i=0; i< ip_arr.length; i++) {
                        $("#serverIp").find("option").each(function(){
                            if ($(this).val() == ip_arr[i]) {
                                $(this).remove();
                            }
                        });
                        $("#serverIp").append("<option value='"+ip_arr[i]+"' selected='selected'>"+ip_arr[i]+"</option>");
                    }
                    $("#serverIp").trigger("chosen:updated");
                },
                error:function(XMLHttpRequest,textStatus,errorThrown){

                }
            });
        }
    }
}
function addInfo() {
    var add = document.getElementById('add_ip');
    var flag = true;
    $("#serverIp").find("option").each(function(){
        if ($(this).val() == add.value) {
            $(this).remove();
        }
    });
    $("#serverIp").append("<option value='"+add.value+"' selected='selected'>"+add.value+"</option>");
    $("#serverIp").trigger("chosen:updated");
}
function changeDomain() {
    var own = document.getElementById("childsys");
    var system = document.getElementById("system");
    var instanceid = document.getElementById("instanceid");
    var domain_link = system.options[system.selectedIndex].value+"_"+own.value+"_"+instanceid.options[instanceid.selectedIndex].value;
    $("#childsys").attr("data-original-title", "鍩熷悕锛�"+domain_link);
    $("#childsys").mouseout();
    $("#childsys").mouseover();
}
function keypress() {
    var str = document.getElementById('value').value;
    regexp =new RegExp(/\n/g);
    var len = str.match(regexp);
    if (len != null) console.log(len.length);
    regexp2 =new RegExp(/[ ]{1,}/g);
    var len2 = str.match(regexp2);
    if (len2 != null) {
        var null_space_len = len2.length;
    } else {
        var null_space_len = 0;
    }
    if (len != null) {
        var change_space_len = len.length;
    } else {
        var change_space_len = 0;
    }
    $("#value").attr("data-original-title","娓╅Θ鎻愮ず锛氬綋鍓嶉厤缃€兼湁 "+null_space_len+"  涓┖鏍�, "+change_space_len+" 涓崲琛�");
    $("#value").mouseout();
    $("#value").mouseover();
}
function space() {
    var str = document.getElementById('value').value;
    regexp =new RegExp(/\n/g);
    var len = str.match(regexp);
    if (len != null) console.log(len.length);
    regexp2 =new RegExp(/[ ]{1,}/g);
    var len2 = str.match(regexp2);
    if (len2 != null) {
        var null_space_len = len2.length;
    } else {
        var null_space_len = 0;
    }
    if (len != null) {
        var change_space_len = len.length;
    } else {
        var change_space_len = 0;
    }
    $("#value").attr("data-original-title","娓╅Θ鎻愮ず锛氬綋鍓嶉厤缃€兼湁 "+null_space_len+"  涓┖鏍�, "+change_space_len+" 涓崲琛�");
}
function getIpListForUpdate() {
    var ip = $("#db_ip_list").val();
    var domain = $("#childsys").val();
    var sharing_domain = $("#sharing_domain").val();
    var db_ip_arr = new Array();
    var ip_arr = new Array();
    db_ip_arr = ip.split(';');
    sharing_domain_arr = sharing_domain.split(',');
    $.ajax({
        type:"POST",
        async:false,
        dataType:"text",
        url:"?m=getIpList",
        data:{"own":domain},
        success:function(data,textStatus){
            var ip_list = new Array();
            ip_list = data.split(';');
            for (var i=0; i < ip_list.length; i++) {
                if ($.inArray(ip_list[i],ip_arr) == -1) {
                    ip_arr.push(ip_list[i]);
                }
            }
        },
        error:function(XMLHttpRequest,textStatus,errorThrown){

        }
    });
    for (var i=0; i < sharing_domain_arr.length; i++) {
        if (sharing_domain_arr[i] == '') continue;
        $.ajax({
            type:"POST",
            async:false,
            dataType:"text",
            url:"?m=getIpList",
            data:{"own":sharing_domain_arr[i]},
            success:function(data,textStatus){
                var ip_list = new Array();
                ip_list = data.split(';');
                for (var i=0; i < ip_list.length; i++) {
                    if ($.inArray(ip_list[i],ip_arr) == -1) {
                        ip_arr.push(ip_list[i]);
                    }
                }
            },
            error:function(XMLHttpRequest,textStatus,errorThrown){

            }
        });
    }
    for (var i=0; i < db_ip_arr.length; i++) {
        var flag = true;
        for (var j=0; j < ip_arr.length; j++) {
            if (db_ip_arr[i] == ip_arr[j]) {
                flag = false;
            }
        }
        if (!flag) {
            $(".chosen-select").append("<option value='"+db_ip_arr[i]+"' selected='selected'>"+db_ip_arr[i]+"</option>");
        } else {
            $(".chosen-select").append("<option value='"+db_ip_arr[i]+"' selected='selected' style='color:red;'>"+db_ip_arr[i]+"</option>");
        }
    }
    for (var i=0; i < ip_arr.length; i++) {
        var flag = true;
        for (var j=0; j < db_ip_arr.length; j++) {
            if (ip_arr[i] == db_ip_arr[j]) {
                flag = false;
            }
        }
        if (flag && (ip_arr[i] != "")) $(".chosen-select").append("<option value='"+ip_arr[i]+"'>"+ip_arr[i]+"</option>");
    }
    $(".chosen-select").trigger("chosen:updated");
}
function getIpListForItemRemoved() {
    var ip = $("#db_ip_list").val();
    var domain = $("#childsys").val();
    var sharing_domain = $("#sharing_domain").val();
    var db_ip_arr = new Array();
    var ip_arr = new Array();
    db_ip_arr = ip.split(';');
    sharing_domain_arr = sharing_domain.split(',');
    $.ajax({
        type:"POST",
        async:false,
        dataType:"text",
        url:"?m=getIpList",
        data:{"own":domain},
        success:function(data,textStatus){
            var ip_list = new Array();
            ip_list = data.split(';');
            for (var i=0; i < ip_list.length; i++) {
                if ($.inArray(ip_list[i],ip_arr) == -1) {
                    ip_arr.push(ip_list[i]);
                }
            }
        },
        error:function(XMLHttpRequest,textStatus,errorThrown){

        }
    });
    for (var i=0; i < sharing_domain_arr.length; i++) {
        if (sharing_domain_arr[i] == '') continue;
        $.ajax({
            type:"POST",
            async:false,
            dataType:"text",
            url:"?m=getIpList",
            data:{"own":sharing_domain_arr[i]},
            success:function(data,textStatus){
                var ip_list = new Array();
                ip_list = data.split(';');
                for (var i=0; i < ip_list.length; i++) {
                    if ($.inArray(ip_list[i],ip_arr) == -1) {
                        ip_arr.push(ip_list[i]);
                    }
                }
            },
            error:function(XMLHttpRequest,textStatus,errorThrown){

            }
        });
    }
    for (var i=0; i < db_ip_arr.length; i++) {
        var flag = true;
        for (var j=0; j < ip_arr.length; j++) {
            if (db_ip_arr[i] == ip_arr[j]) {
                flag = false;
            }
        }
        if (!flag) {
            $(".chosen-select").append("<option value='"+db_ip_arr[i]+"' selected='selected'>"+db_ip_arr[i]+"</option>");
        } else {
            $(".chosen-select").append("<option value='"+db_ip_arr[i]+"' style='color:red;'>"+db_ip_arr[i]+"</option>");
        }
    }
    for (var i=0; i < ip_arr.length; i++) {
        var flag = true;
        for (var j=0; j < db_ip_arr.length; j++) {
            if (ip_arr[i] == db_ip_arr[j]) {
                flag = false;
            }
        }
        if (flag && (ip_arr[i] != "")) $(".chosen-select").append("<option value='"+ip_arr[i]+"' selected='selected'>"+ip_arr[i]+"</option>");
    }
    $(".chosen-select").trigger("chosen:updated");
}